package com.thread;

import java.util.concurrent.CountDownLatch;

public class CountDownLatchTest {

    //实例化 CountDownLatch, 线程数量为 4
    static final CountDownLatch latch = new CountDownLatch(4);
    public static void main(String[] args) {
        System.out.println("开始准备赛跑，所有参赛者准备");
        new Thread(() -> {
            try {
                Thread.sleep(2000);
                latch.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("第一个人跑完了，一片欢呼声~~~~~~~~~~~");
        }).start();
        System.out.print("第一个人开始跑了|");
        new Thread(() -> {
            try {
                Thread.sleep(4000);
                latch.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
                System.out.println("第二个人跑完了，好多人鼓掌~~~~~~~~~~~");
        }).start();
        System.out.print("第二个人开始跑了|");
        new Thread(() -> {
            try {
                Thread.sleep(5000);
                latch.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
                System.out.println("第三个人跑完了，没几个人鼓掌~~~~~~~~~~~");
        }).start();
        System.out.print("第三个人开始跑了|");
        new Thread(() -> {
            try {
                Thread.sleep(7000);
                latch.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("第四个人跑完了~~~~~~~~~~~");
        }).start();
        System.out.println("第四个人开始跑了");
        System.out.println("发令枪~~~~pong...");
        System.out.println("裁判员在等待着...");
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("赛跑项目结束了。");
    }

}
